package cn.edu.tsinghua.training.cms.demo.service.impl;

import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.edu.tsinghua.training.cms.demo.dao.GenericDao;
import cn.edu.tsinghua.training.cms.demo.entity.Log;
import cn.edu.tsinghua.training.cms.demo.service.LogService;

@Service
@Aspect
public class LogServiceImpl extends GenericServiceImpl<Log, Integer> implements LogService {

	@Override
	@Autowired
	@Qualifier("logDaoImpl")
	public void setGenericDao(GenericDao<Log, Integer> genericDao) {
		super.setGenericDao(genericDao);
	}

	@Override
	@Before("execution(* cn.edu.tsinghua.training.cms.demo.service.impl.*.*(..))")
	public void before(JoinPoint joinPoint) {
		String operation = joinPoint.getSignature().getName();
		Object[] objects = joinPoint.getArgs();
		Object detail = null;
		for (Object object : objects) {
			System.out.println("--->" + object);
			detail = object;
		}
		Date time = new Date();
		System.out.println("before: " + operation + time);
		Log log = new Log(null, operation, time, "", null);//detail.toString(), null);
		genericDao.add(log);
	}

	@Override
	@After("execution(* cn.edu.tsinghua.training.cms.demo.service.impl.*.*(..))")
	public void after(JoinPoint joinPoint) {
		String operation = joinPoint.getSignature().getName();
		System.out.println("after: " + operation);
	}
	
}